local tunjiang = fk.CreateSkill {
  name = "lingling__tunjiang",
}

Fk:loadTranslationTable{
  ["lingling__tunjiang"] = "吞江",
  [":lingling__tunjiang"] = "每轮限一次，其他角色额定摸牌阶段结束时，你可以与其各执行一个额外的弃牌阶段，若其因此弃置至少三张牌，你失去此技能。",

  ["#lingling__tunjiang-invoke"] = "吞江：是否与 %dest 各执行一个弃牌阶段？若其此阶段弃置至少三张牌则你失去本技能",
}

tunjiang:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(tunjiang.name) and target.phase == Player.Draw and
      not target.dead and player:usedSkillTimes(tunjiang.name, Player.HistoryRound) == 0 and data.reason == "game_rule"
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = tunjiang.name,
      prompt = "#lingling__tunjiang-invoke::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    target:gainAnExtraPhase(Player.Discard, tunjiang.name)
    player:gainAnExtraPhase(Player.Discard, tunjiang.name)
  end,
})

tunjiang:addEffect(fk.EventPhaseEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if target ~= player and target.phase == Player.Discard and data.reason == tunjiang.name and
      player:usedSkillTimes(tunjiang.name, Player.HistoryTurn) > 0 then
      local n = 0
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from == target and move.moveReason == fk.ReasonDiscard then
            n = n + #move.moveInfo
          end
        end
      end, Player.HistoryPhase)
      return n > 2
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:handleAddLoseSkills(player, "-lingling__tunjiang")
  end,
})

return tunjiang
